<?php
/**
 * Created by PhpStorm.
 * User: caijianling
 * Date: 2018/8/16
 * Time: 下午3:38
 */

class TOAdminSQL extends fileFunctions
{

    public $conn;

    function __construct()
    {
        // 1.链接数据库
        if ($_SERVER['HTTP_HOST'] == 'to.hdacgn.com') $passwd = '1935201a7515b830'; else $passwd = 'root';
        $conn = @new mysqli('localhost', 'root', $passwd, 'three_one');
        if ($conn->connect_error) {//返回链接错误号
            // 返回链接错误信息
            die('数据库链接错误 (' . $conn->connect_errno . ') '
                . $conn->connect_error);
        }
        // 2. 选择数据库
        $conn->select_db('three_one') or die('选择数据库失败：' . $conn->error);
        $conn->set_charset('utf8');
        $conn->query("set character set 'utf8'");
        $conn->query("set names 'utf8'");
        $this->conn = $conn;
    }

    // 注册
    public function sign_up($email, $name, $password, $password_again, $read, $role, $group)
    {
        if ($email === '') return ['status' => 1, 'error' => 'free email'];
        if ($name === '') return ['status' => 1, 'error' => 'free name'];
        if ($password === '') return ['status' => 1, 'error' => 'free password'];
        if ($password_again === '') return ['status' => 1, 'error' => 'free password_again'];
        if ($read !== 1) return ['status' => 1, 'error' => 'not read'];
        if ($password !== $password_again) return ['status' => 1, 'error' => 'not same password'];
        if (preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/', $email)) return ['status' => 1, 'error' => 'not a real email'];
        $name = addslashes($name);
        $password = addslashes(password_hash($password, PASSWORD_DEFAULT));
        $email = addslashes($email);
        $select_email_query = "SELECT COUNT(1) FROM `to_admin_user` WHERE `email` = '$email'";
        $select_email_res = $this->conn->query($select_email_query);
        if (!$select_email_res) return ['status' => 1, 'error' => 's email:' . $this->sql_error()];
        $select_email_row = $select_email_res->fetch_row();
        if ($select_email_row[0] !== 0) return ['status' => 1, 'error' => 'had email'];
        $select_email_res->free();
        $select_name_query = "SELECT COUNT(1) FROM `to_admin_user` WHERE `username` = '$name'";
        $select_name_res = $this->conn->query($select_name_query);
        if (!$select_name_res) return ['status' => 1, 'error' => 's name:' . $this->sql_error()];
        $select_username_row = $select_name_res->fetch_row();
        if ($select_username_row[0] !== 0) return ['status' => 1, 'error' => 'had username'];
        $select_name_res->free();
        $ip = addslashes($this->get_ip());
        $time = time();
        $insert_query = "INSERT INTO `to_admin_user` (`username`, `password`, `email`, `role`, `group`, `signup_ip`, `create_time`, `update_time`) VALUES ('$name', '$password', '$email', '$role', '$group', '$ip', '$time', $time)";
        $insert_res = $this->conn->query($insert_query);
        if (!$insert_res) return ['status' => 1, 'error' => 'insert:' . $this->sql_error()];
        return ['status' => 0, 'error' => 0];
    }

    // 登录
    public function login($name, $password, $remember)
    {
        if ($name === '') return ['status' => 1, 'error' => 'free name'];
        if ($password === '') return ['status' => 1, 'error' => 'free password'];
        if ($remember != 0 && $remember != 1) return ['status' => 1, 'error' => 'free remember'];
        $name = addslashes($name);
        $select_name_query = "SELECT COUNT(1) FROM `to_admin_user` WHERE `username` = '$name'";
        $select_name_res = $this->conn->query($select_name_query);
        if (!$select_name_res) return ['status' => 1, 'error' => 's name:' . $this->sql_error()];
        $select_name_row = $select_name_res->fetch_row();
        if ($select_name_row[0] < 1) return ['status' => 1, 'error' => 'no name'];
        $select_id_password_query = "SELECT `id`, `password` FROM `to_admin_user` WHERE `username` = '$name'";
        $select_id_password_res = $this->conn->query($select_id_password_query);
        if (!$select_id_password_res) return ['status' => 1, 'error' => 's password:' . $this->sql_error()];
        $select_id_password_row = $select_id_password_res->fetch_row();
        $select_password = $select_id_password_row[1];
        if (!password_verify($password, $select_password)) return ['status' => 1, 'error' => 'fail password'];
        $select_id = $select_id_password_row[0];
        $time = time();
        $ip = addslashes($this->get_ip());
        $update_query = "UPDATE `to_admin_user` SET `last_login_ip` = '$ip', `last_login_time` = $time WHERE `id` = $select_id";
        $res = $this->conn->query($update_query);
        if (!$res) return ['status' => 1, 'error' => $this->sql_error()];
        return ['status' => 0, 'error' => 0, 'uid' => $select_id, 'remember' => $remember];
    }

    // 已知uid获取name
    public function uid_name($uid)
    {
        $uid = addslashes($uid);
        $select_query = "SELECT `username` FROM `to_admin_user` WHERE `id` = '$uid'";
        $select_res = $this->conn->query($select_query);
        if ($select_res) {
            $select_row = $select_res->fetch_row();
            return $select_row[0];
        } else return $this->sql_error();
    }

    public function about_us_select($start, $num)
    {
        $start = (int)$start;
        $num = (int)$num;
        $start = $start - 1;
        $select_query = "SELECT `id`, `name`, `head`, `job`, `author`, `time` FROM `to_about_set` ORDER BY `id` DESC LIMIT $start, $num";
        $select_res = $this->conn->query($select_query);
        if (!$select_res) return ['status' => 1, 'error' => $this->sql_error()];
        $select_num = $select_res->num_rows;
        $select_array = [];
        for ($i = 0; $i < $select_num; $i++) {
            $select_assoc = $select_res->fetch_assoc();
            $select_array[] = $select_assoc;
        }
        return ['status' => 0, 'num' => $select_num, 'array' => $select_array];
    }

    public function about_us_insert($json, $auth_id, $num)
    {
        $add_array = json_decode($json, true);
        if (count($add_array) < $num) return ['status' => 1, 'error' => 'don\'t have ' . $num . ' items in'];
        $name = addslashes($add_array['name']);
        $head = addslashes($add_array['head']);
        $job = addslashes($add_array['job']);
        $auth_id = addslashes($auth_id);
        $now_time = time();
        $insert_query = "INSERT INTO `to_about_set` (`name`, `head`, `job`, `author`, `time`) VALUES ('$name', '$head', '$job', '$auth_id', '$now_time')";
        $insert_res = $this->conn->query($insert_query);
        if (!$insert_res) return ['status' => 1, 'error' => $this->sql_error()];
        return ['status' => 0, 'error' => 0];
    }

    public function index_swipe_insert($json, $auth_id)
    {
        $add_array = json_decode($json, true);
        if (count($add_array) < 3) return ['status' => 1, 'error' => 'don\'t have 4 items in'];
        $image = addslashes($add_array['image']);
        $title = addslashes($add_array['title']);
        $content = addslashes($add_array['content']);
        $url = addslashes($add_array['url']);
        $auth_id = addslashes($auth_id);
        $now_time = time();
        $insert_query = "INSERT INTO `to_swipe_img` (`image`, `title`, `content`, `url`, `author`, `time`) VALUES ('$image', '$title', '$content', '$url', '$auth_id', '$now_time')";
        $insert_res = $this->conn->query($insert_query);
        if (!$insert_res) return ['status' => 1, 'error' => $this->sql_error()];
        return ['status' => 0, 'error' => 0];
    }

    public function index_show_insert($json, $auth_id)
    {
        $add_array = json_decode($json, true);
        if (count($add_array) < 3) return ['status' => 1, 'error' => 'don\'t have 4 items in'];
        $image = addslashes($add_array['image']);
        $title = addslashes($add_array['title']);
        $alt = addslashes($add_array['alt']);
        $url = addslashes($add_array['url']);
        $auth_id = addslashes($auth_id);
        $now_time = time();
        $insert_query = "INSERT INTO `to_show_img` (`image`, `title`, `alt`, `url`, `author`, `time`) VALUES ('$image', '$title', '$alt', '$url', '$auth_id', '$now_time')";
        $insert_res = $this->conn->query($insert_query);
        if (!$insert_res) return ['status' => 1, 'error' => $this->sql_error()];
        return ['status' => 0, 'error' => 0];
    }

    public function index_news_insert($json, $auth_id)
    {
        $add_array = json_decode($json, true);
        if (count($add_array) < 3) return ['status' => 1, 'error' => 'don\'t have 4 items in'];
        $title = addslashes($add_array['title']);
        $image = addslashes($add_array['image']);
        $content = addslashes($add_array['content']);
        $url = addslashes($add_array['url']);
        $auth_id = addslashes($auth_id);
        $now_time = time();
        $insert_query = "INSERT INTO `to_index_news` (`title`, `content`, `image`, `url`, `author`, `time`) VALUES ('$title', '$content', '$image', '$url', '$auth_id', '$now_time')";
        $insert_res = $this->conn->query($insert_query);
        if (!$insert_res) return ['status' => 1, 'error' => $this->sql_error()];
        return ['status' => 0, 'error' => 0];
    }

    public function news_insert($json, $auth_id)
    {
        $add_array = json_decode($json, true);
        if (count($add_array) < 4) return ['status' => 1, 'error' => 'don\'t have 4 items in'];
        $title = addslashes($add_array['title']);
        $show_image = addslashes($add_array['show_image']);
        $show_content = addslashes($add_array['show_content']);
        $content = addslashes($add_array['content']);
        $auth_id = addslashes($auth_id);
        $now_time = time();
        $insert_query = "INSERT INTO `to_news_set` (`title`, `show_image`, `show_content`, `content`, `author`, `time`) VALUES ('$title', '$show_image', '$show_content', '$content', '$auth_id', '$now_time')";
        $insert_res = $this->conn->query($insert_query);
        if (!$insert_res) return ['status' => 1, 'error' => $this->sql_error()];
        return ['status' => 0, 'error' => 0];
    }

    public function role_insert($json)
    {
        $add_array = json_decode($json, true);
        if (count($add_array) < 5) return ['status' => 1, 'error' => 'don\'t have 5 items in'];
        $name = addslashes($add_array['name']);
        $des = addslashes($add_array['des']);
        $access = addslashes($add_array['access']);
        $sort = addslashes($add_array['sort']);
        $mod = addslashes($add_array['mod']);
        $now_time = time();
        $insert_query = "INSERT INTO `to_admin_role` (`pid`, `name`, `description`, `menu_auth`, `sort`, `create_time`, `update_time`, `status`, `access`, `default_module`) VALUES ('1', '$name', '$des', '', '$sort', '$now_time', '$now_time', '1', '$access', '$mod')";
        $insert_res = $this->conn->query($insert_query);
        if (!$insert_res) return ['status' => 1, 'error' => $this->sql_error()];
        return ['status' => 0, 'error' => 0];
    }

    public function build_html_select($start, $num)
    {
        $start = (int)$start;
        $num = (int)$num;
        $start = $start - 1;
        $select_query = "SELECT `id`, `name`, `html`, `author`, `time`, `use` FROM `to_build_html` ORDER BY `id` ASC LIMIT $start, $num";
        $select_res = $this->conn->query($select_query);
        if (!$select_res) return ['status' => 1, 'error' => $this->sql_error()];
        $select_num = $select_res->num_rows;
        $select_array = [];
        for ($i = 0; $i < $select_num; $i++) {
            $select_assoc = $select_res->fetch_assoc();
            $select_array[] = $select_assoc;
        }
        return ['status' => 0, 'num' => $select_num, 'array' => $select_array];
    }

    public function index_swipe_select($start, $num)
    {
        $start = (int)$start;
        $num = (int)$num;
        $start = $start - 1;
        $select_query = "SELECT `id`, `image`, `title`, `content`, `url`, `author`, `time` FROM `to_swipe_img` ORDER BY `id` DESC LIMIT $start, $num";
        $select_res = $this->conn->query($select_query);
        if (!$select_res) return ['status' => 1, 'error' => $this->sql_error()];
        $select_num = $select_res->num_rows;
        $select_array = [];
        for ($i = 0; $i < $select_num; $i++) {
            $select_assoc = $select_res->fetch_assoc();
            $select_array[] = $select_assoc;
        }
        return ['status' => 0, 'num' => $select_num, 'array' => $select_array];
    }

    public function index_show_select($start, $num)
    {
        $start = (int)$start;
        $num = (int)$num;
        $start = $start - 1;
        $select_query = "SELECT `id`, `image`, `alt`, `title`, `url`, `author`, `time` FROM `to_show_img` ORDER BY `id` DESC LIMIT $start, $num";
        $select_res = $this->conn->query($select_query);
        if (!$select_res) return ['status' => 1, 'error' => $this->sql_error()];
        $select_num = $select_res->num_rows;
        $select_array = [];
        for ($i = 0; $i < $select_num; $i++) {
            $select_assoc = $select_res->fetch_assoc();
            $select_array[] = $select_assoc;
        }
        return ['status' => 0, 'num' => $select_num, 'array' => $select_array];
    }

    public function index_news_select($start, $num)
    {
        $start = (int)$start;
        $num = (int)$num;
        $start = $start - 1;
        $select_query = "SELECT `id`, `title`, `content`, `image`, `url`, `author`, `time` FROM `to_index_news` ORDER BY `id` DESC LIMIT $start, $num";
        $select_res = $this->conn->query($select_query);
        if (!$select_res) return ['status' => 1, 'error' => $this->sql_error()];
        $select_num = $select_res->num_rows;
        $select_array = [];
        for ($i = 0; $i < $select_num; $i++) {
            $select_assoc = $select_res->fetch_assoc();
            $select_array[] = $select_assoc;
        }
        return ['status' => 0, 'num' => $select_num, 'array' => $select_array];
    }

    public function news_select($start, $num)
    {
        $start = (int)$start;
        $num = (int)$num;
        $start = $start - 1;
        $select_query = "SELECT `id`, `title`, `show_image`, `show_content`, `content`, `author`, `time` FROM `to_news_set` ORDER BY `id` DESC LIMIT $start, $num";
        $select_res = $this->conn->query($select_query);
        if (!$select_res) return ['status' => 1, 'error' => $this->sql_error()];
        $select_num = $select_res->num_rows;
        $select_array = [];
        for ($i = 0; $i < $select_num; $i++) {
            $select_assoc = $select_res->fetch_assoc();
            $select_array[] = $select_assoc;
        }
        return ['status' => 0, 'num' => $select_num, 'array' => $select_array];
    }

    public function users_select($start, $num)
    {
        $start = (int)$start;
        $num = (int)$num;
        $start = $start - 1;
        $select_query = "SELECT `id`, `username`, `nickname`, `email`, `email_bind`, `mobile`, `mobile_bind`, `avatar`, `money`, `score`, `role`, `group`, `signup_ip`, `create_time`, `create_time`, `update_time`, `last_login_time`, `last_login_ip`, `sort`, `status` FROM `to_admin_user` ORDER BY `id` DESC LIMIT $start, $num";
        $select_res = $this->conn->query($select_query);
        if (!$select_res) return ['status' => 1, 'error' => $this->sql_error()];
        $select_num = $select_res->num_rows;
        $select_array = [];
        for ($i = 0; $i < $select_num; $i++) {
            $select_assoc = $select_res->fetch_assoc();
            $select_array[] = $select_assoc;
        }
        return ['status' => 0, 'num' => $select_num, 'array' => $select_array];
    }

    public function role_select($start = 1, $num = 9999)
    {
        $start = (int)$start - 1;
        $num = (int)$num;
        $select_query = "SELECT `id`, `pid`, `name`, `description`, `menu_auth`, `sort`, `create_time`, `update_time`, `status`, `access`, `default_module` FROM `to_admin_role` ORDER BY `sort` ASC LIMIT $start, $num";
        $select_res = $this->conn->query($select_query);
        if (!$select_res) return ['status' => 1, 'error' => $this->sql_error()];
        $select_num = $select_res->num_rows;
        $select_assoc = [];
        for ($i = 0; $i < $select_num; $i++) {
            $select_assoc[] = $select_res->fetch_assoc();
        }
        return ['status' => 0, 'num' => $select_num, 'assoc' => $select_assoc];
    }

    public function add_news_html($id)
    {
        $id = addslashes($id);
        $select_query = "SELECT `title`, `content`, `time` FROM `to_news_set` WHERE `id` = '$id'";
        $select_res = $this->conn->query($select_query);
        if (!$select_res) return ['status' => 1, 'error' => 1];
        $select_assoc = $select_res->fetch_assoc();
        $title = $select_assoc['title'];
        $content = $select_assoc['content'];
        $time = $select_assoc['time'];
        $date = date('Y-m-d', $time);
        return ['status' => 0, 'title' => $title, 'date' => $date, 'content' => $content];
    }

    // $parent is the parent of the children we want to see
    // $level is increased when we go deeper into the tree,
    //        used to display a nice indented tree
    public function display_children($parent, $level)
    {
        static $s_array = [];
        // 获得一个 父节点 $parent 的所有子节点
        $result = $this->conn->query('SELECT `name` FROM `to_php_tree` WHERE `parent` = "' . $parent . '";');

        $array = [];

        // 显示每个子节点
        while ($row = $result->fetch_assoc()) {
            $array[$parent] = $row['name'];
            // 缩进显示节点名称
            echo str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $level) . $row['name'] . "<br>";

            //再次调用这个函数显示子节点的子节点

            $this->display_children($row['name'], $level + 1);
        }
        $s_array[] = $array;
        $this->a = $s_array;
    }

    // $node 是那个最深的节点
    public function get_path($node)
    {
        // 查询这个节点的父节点
        $result = $this->conn->query('SELECT `parent` FROM `to_php_tree` WHERE `name` = "' . $node . '";');
        $row = $result->fetch_assoc();

        // 用一个数组保存路径
        $path = array();

        // 如果不是根节点则继续向上查询
        // (根节点没有父节点)
        if ($row['parent'] != '') {
            // the last part of the path to $node, is the name
            // of the parent of $node
            $path[] = $row['parent'];

            // we should add the path to the parent of this node
            // to the path
            $path = array_merge($this->get_path($row['parent']), $path);
        }

        // return the path
        return $path;
    }

    public function build_news()
    {
        date_default_timezone_set('PRC');
        // 获取config中$config_website_set的信息
        $web_set_res = $this->config_variable_get('./admin-core/config.php', 'config_website_set');
        if ($web_set_res['status'] !== 0) return $web_set_res;
        $web_set_assoc = $web_set_res['assoc'];
        // 获取新闻信息
        $news_res = $this->news_select(1, 50);
        if ($news_res['status'] !== 0) return $news_res;
        $news_num = $news_res['num'];
        $news_assoc = $news_res['array'];
        ob_start();
        require_once './diy-html/news.php';
        $content = ob_get_contents();
        ob_end_clean();
        if ($this->wFile('../news.html', $content)) {
            $time = time();
            $update_query = "UPDATE `to_build_html` SET `time` = $time WHERE `html` = 'news.html'";
            $this->conn->query($update_query);
            return [true, date('Y年m月d日 h:i:s', $time)];
        } else return [false];
    }

    //不同环境下获取真实的IP
    public function get_ip()
    {
        static $real_ip = NULL;
        if ($real_ip !== NULL) {
            return $real_ip;
        }
        //判断服务器是否允许$_SERVER
        if (isset($_SERVER)) {
            if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $real_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
                $real_ip = $_SERVER['HTTP_CLIENT_IP'];
            } else {
                $real_ip = $_SERVER['REMOTE_ADDR'];
            }
        } else {
            //不允许就使用getenv获取
            if (getenv("HTTP_X_FORWARDED_FOR")) {
                $real_ip = getenv("HTTP_X_FORWARDED_FOR");
            } elseif (getenv("HTTP_CLIENT_IP")) {
                $real_ip = getenv("HTTP_CLIENT_IP");
            } else {
                $real_ip = getenv("REMOTE_ADDR");
            }
        }
        return $real_ip;
    }

    // sqlerror
    protected function sql_error()
    {
        return $this->conn->error;
    }

    function __destruct()
    {
        // TODO: Implement __destruct() method.
        // 关闭数据库连接
        $this->conn->close();
    }
}

class fileFunctions
{
    // 创建文件夹
    public function makeDir($dir)
    {
        // 如果文件夹不存在，将以递归方式创建该文件夹
        is_dir($dir) OR mkdir($dir, 0777, true);
    }

    public function wFile($file, $content)
    {
        if (file_put_contents($file, $content))
            return true;
        else
            return false;
    }

    public function rFile($file)
    {
        if ($content = file_get_contents($file))
            return $content;
        else
            return false;
    }

    //配置文件数据值获取。
    //默认没有第三个参数时，按照字符串读取提取''中或""中的内容
    //如果有第三个参数时为int时按照数字int处理。
    public function config_get($file, $ini, $type = 'string')
    {
        if ($type == 'int') {
            $str = file_get_contents($file);
            $config = preg_match('/' . $ini . '\s=\s(.*);/', $str, $res);
            return stripslashes($res[1]);
        } else {
            $str = file_get_contents($file);
            $config = preg_match('/' . $ini . '\s=\s\'(.*)\';/', $str, $res);
            if (isset($res[1])) return stripslashes($res[1]); else return '';
        }
    }

    //配置文件数据项更新
    //默认没有第四个参数时，按照字符串读取提取''中或""中的内容
    //如果有第四个参数时为int时按照数字int处理。
    public function config_update($file, $ini, $value, $type = 'string')
    {
        $a_value = addslashes($value);
        $str = file_get_contents($file);
        $str2 = '';
        if ($type == 'int') {
            $str2 = preg_replace('/' . $ini . '\s=\s(.*);/', $ini . ' = ' . $a_value . ';', $str);
        } elseif ($type == 'variable') {
            $str2 = preg_replace('/' . $ini . '\s=\s"(.*)";/', $ini . ' = \'' . $a_value . '\';', $str);
        } else {
            $str2 = preg_replace('/' . $ini . '\s=\s\'(.*)\';/', $ini . ' = \'' . $a_value . '\';', $str);
        }
        file_put_contents($file, $str2);
        $test_get = $this->config_get($file, $ini, $type);
        if ($test_get !== $value) {
            $test_set = $this->config_add($file, $ini, $value, $type);
            if ($test_set !== $value) return false; else return true;
        } else return true;
    }

    public function config_add($file, $ini, $value, $type = 'string')
    {
        $a_value = addslashes($value);
        $str = file_get_contents($file);
        if ($type == 'int') {
            $str2 = $str . "\r\n$" . $ini . ' = ' . $a_value . ';';
        } elseif ($type == 'variable') {
            $str2 = $str . "\r\n$" . $ini . ' = "' . $a_value . '";';
        } else {
            $str2 = $str . "\r\n$" . $ini . ' = \'' . $a_value . '\';';
        }
        file_put_contents($file, $str2);
        $test_get = $this->config_get($file, $ini, $type);
        if ($test_get !== $value) return false;
        return true;
    }

    // config读取
    public function config_variable_get($file, $variable)
    {
        $res = $this->config_get($file, $variable);
        if (empty($res)) return ['status' => 1, 'error' => 'empty'];
        $res_assoc = json_decode($res, true);
        return ['status' => 0, 'assoc' => $res_assoc];
    }

    // config写入
    public function config_variable_set($file, $json, $variable, $items)
    {
        $array = json_decode($json, true);
        $count = count($array);
        if ($count < $items) return ['status' => 1, 'error' => 'don\'t have ' . $items . ' items'];
        $res = $this->config_update($file, $variable, $json);
        if (!$res) return ['status' => 1, 'error' => 1];
        return ['status' => 0, 'error' => 0];
    }

    public function set_auto_about()
    {
        $get_array = $this->config_variable_get('./admin-core/config.php', 'about_us_set');
        if ($get_array['status'] === 1) return $get_array;
        $assoc_array = $get_array['assoc'];
        $image1 = $assoc_array['image2'];
        $image2 = $assoc_array['image1'];
        preg_match('/(.*)<bn\/><bn\/>(.*)/', $assoc_array['intro'], $intro);
        return ['status' => 0, 'array' => ['image' => [$image1, $image2], 'content' => $intro[1]]];
    }

    public function build_about()
    {
        date_default_timezone_set('PRC');
        // 获取config中$config_website_set的信息
        $sql = new TOAdminSQL();
        $web_set_res = $this->config_variable_get('./admin-core/config.php', 'config_website_set');
        if ($web_set_res['status'] !== 0) return $web_set_res;
        $web_set_assoc = $web_set_res['assoc'];
        // 获取config中$about_us_set的信息
        $about_set_res = $this->config_variable_get('./admin-core/config.php', 'about_us_set');
        if ($about_set_res['status'] !== 0) return $about_set_res;
        $about_set_assoc = $about_set_res['assoc'];
        // 获取团队成员信息
        $about_us_res = $sql->about_us_select(1, 50);
        if ($about_us_res['status'] !== 0) return $about_us_res;
        $about_us_num = $about_us_res['num'];
        $about_us_assoc = $about_us_res['array'];
        ob_start();
        require_once './diy-html/about.php';
        $content = ob_get_contents();
        ob_end_clean();
        if ($this->wFile('../about.html', $content)) {
            $time = time();
            $update_query = "UPDATE `to_build_html` SET `time` = $time WHERE `html` = 'about.html'";
            $sql->conn->query($update_query);
            return [true, date('Y年m月d日 h:i:s', $time)];
        } else return [false];
    }

    public function build_index()
    {
        date_default_timezone_set('PRC');
        // 获取config中$config_website_set的信息
        $web_set_res = $this->config_variable_get('./admin-core/config.php', 'config_website_set');
        if ($web_set_res['status'] !== 0) return $web_set_res;
        $web_set_assoc = $web_set_res['assoc'];
        // 获取config中$index_about_set的信息
        $index_about_set = $this->config_variable_get('./admin-core/config.php', 'index_about_set');
        if ($index_about_set['status'] !== 0) return $index_about_set;
        $index_about_assoc = $index_about_set['assoc'];
        //
        $sql = new TOAdminSQL();
        // 首页轮播图:
        $index_swipe_res = $sql->index_swipe_select(1, 999);
        $index_swipe_num = $index_swipe_res['num'];
        $index_swipe_array = $index_swipe_res['array'];
        // 第三面方块展示
        $index_show_set = $sql->index_show_select(1, 10);
        if ($index_show_set['status'] !== 0) return $index_show_set;
        $index_show_num = $index_show_set['num'];
        $index_show_array = $index_show_set['array'];
        // 第四方块新闻预览
        $index_news_set = $sql->index_news_select(1, 3);
        if ($index_news_set['status'] !== 0) return $index_news_set;
        $index_news_num = $index_news_set['num'];
        $index_news_array = $index_news_set['array'];
        ob_start();
        require_once './diy-html/index.php';
        $content = ob_get_contents();
        ob_end_clean();
        if ($this->wFile('../index.html', $content)) {
            $time = time();
            $update_query = "UPDATE `to_build_html` SET `time` = $time WHERE `html` = 'index.html'";
            $sql->conn->query($update_query);
            return [true, date('Y年m月d日 h:i:s', $time)];
        } else return [false];
    }

    public function build_sth($html)
    {
        $admin_sql = new TOAdminSQL();
        switch ($html) {
            case 'index.html':
                $res = $this->build_index();
                if ($res[0] === true)
                    return json_encode(['status' => 0, 'time' => $res[1]]);
                else
                    return json_encode(['status' => 1, 'error' => 1]);
                break;
            case 'about.html':
                $res = $this->build_about();
                if ($res[0] === true)
                    return json_encode(['status' => 0, 'time' => $res[1]]);
                else
                    return json_encode(['status' => 1, 'error' => 1]);
                break;
            case 'news.html':
                $res = $admin_sql->build_news();
                if ($res[0] === true)
                    return json_encode(['status' => 0, 'time' => $res[1]]);
                else
                    return json_encode(['status' => 1, 'error' => 1]);
                break;
            default:
                exit(json_encode(['status' => 1, 'error' => 1]));
        }
    }

    /**
     * 随机数命名
     * @param int $size 复杂程度
     * @param int $length 长度
     * @return string
     */
    public function fileReName($size = 6, $length = 10)
    {
        //创建随机数，用来给二维码命名
        $numbers = range(0, 9);
        $letters_s = range('a', 'z');
        $letters_b = range('A', 'Z');
        switch ($size) {
            case 0:
                $changeStr = join('', $numbers);
                break;
            case 1:
                $changeStr = join('', $letters_s);
                break;
            case 2:
                $changeStr = join('', $letters_b);
                break;
            case 3:
                $changeStr = join("", array_merge($numbers, $letters_s));
                break;
            case 4:
                $changeStr = join("", array_merge($numbers, $letters_b));
                break;
            case 5:
                $changeStr = join("", array_merge($letters_s, $letters_b));
                break;
            case 6:
                $changeStr = join('', array_merge($numbers, $letters_s, $letters_b));
                break;
            default :
                $changeStr = join('', array_merge($numbers, $letters_s, $letters_b));
        }
        $outName = str_shuffle($changeStr){0};
        for ($i = 0; $i < $length; $i++) {
            $outName .= str_shuffle($changeStr){0};
        }
        return $outName;
    }

    // 生成唯一值
    public function makeOwnValue()
    {
        return md5(uniqid(rand(), 1));
    }
}